package com.douguo.recipe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.douguo.recipe.network.Constant;
import com.douguo.recipe.object.Sort;
import com.douguo.recipe.provider.DBHelper;
import com.douguo.recipe.view.NavigationView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SortActivity extends DouguoActivity {
	// ----------data------------
	private List<Sort> mCaixis;
	private List<Sort> mCaipins;
	private List<Sort> mTeses;
	private List<Sort> mKouweis;
	
	private List<Map<String, Object>> mData;
	private int mCurrentType = MINOR_TYPE_NONE;
	
	// ----------DBHelper--------
	private DBHelper mDBHelper;
	
	//-----------UI--------------
	private LinearLayout mLayoutSortMain;//一级目录
	private ListView mLayoutSortMinor;//二级目录
	
	private LinearLayout mLayoutCaipin;
	private LinearLayout mLayoutCaixi;
	private LinearLayout mLayoutTese;
	private LinearLayout mLayoutKouwei;
	private NavigationView mNavigation;
	
	// ------------adapter-------
	private SortAdapter mAdapter;
	
	//---------constant-------------
	private static final int MINOR_TYPE_NONE = -1;
	private static final int MINOR_TYPE_CAIPIN = Constant.TYPE_CAIPIN;
	private static final int MINOR_TYPE_CAIXI = Constant.TYPE_CAIXI;
	private static final int MINOR_TYPE_TESE = Constant.TYPE_TESE;
	private static final int MINOR_TYPE_KOUWEI = Constant.TYPE_KOUWEI;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sort);
		mDBHelper = new DBHelper(this);
		initData();
		initUI();
		setTitle(getString(R.string.titleCaiPuFenLei));
	}
	
	public void initData() {
		mCaixis = mDBHelper.getSorts(Constant.TYPE_CAIXI);
		mCaipins = mDBHelper.getSorts(Constant.TYPE_CAIPIN);
		mTeses = mDBHelper.getSorts(Constant.TYPE_TESE);
		mKouweis = mDBHelper.getSorts(Constant.TYPE_KOUWEI);
		
		
	}
	

	public void initUI() {
		mNavigation = new NavigationView(this, NavigationView.STATE_SORT);
		mLayoutSortMain = (LinearLayout) findViewById(R.id.sort_main);
		mLayoutSortMinor = (ListView) findViewById(R.id.sort_minor);
		
		mLayoutCaipin = (LinearLayout) findViewById(R.id.layout_sort_caipin);
		mLayoutCaipin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showSortMinor(MINOR_TYPE_CAIPIN);
				
			}
		});
		mLayoutCaixi = (LinearLayout) findViewById(R.id.layout_sort_caixi);
		mLayoutCaixi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showSortMinor(MINOR_TYPE_CAIXI);
				
			}
		});
		mLayoutTese = (LinearLayout) findViewById(R.id.layout_sort_tese);
		mLayoutTese.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showSortMinor(MINOR_TYPE_TESE);
				
			}
		});
		
		mLayoutKouwei = (LinearLayout) findViewById(R.id.layout_sort_kouwei);
		mLayoutKouwei.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showSortMinor(MINOR_TYPE_KOUWEI);
				
			}
		});
		
		mData = new ArrayList<Map<String, Object>>();
		mAdapter = new SortAdapter(this, mData, R.layout.sort_listitem, new String[]{"name","id"}, 
					new int[]{R.id.sort_listitem_text});
		mLayoutSortMinor.setAdapter(mAdapter);
		
		findViewById(R.id.imageButton_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				back();
				
			}
		});
		
		mLayoutSortMinor.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(SortActivity.this,
						RecipelistActivity.class);
				Bundle bundle = new Bundle();
				bundle.putIntArray("types", new int[] { mCurrentType });
				bundle.putStringArray("ids",
						new String[] { String.valueOf(id) });
				bundle.putString("title", (String) mData.get(position).get("name"));
				intent.putExtras(bundle);
				startActivity(intent);
				
			}
		});
		
		showSortMain();
		
	}
	
	public void showSortMain() {
		mLayoutSortMain.setVisibility(View.VISIBLE);
		mLayoutSortMinor.setVisibility(View.GONE);
		mCurrentType = MINOR_TYPE_NONE;
	}
	
	public void showSortMinor(int type) {
		mLayoutSortMinor.post(new Runnable() {
			
			@Override
			public void run() {
				mLayoutSortMinor.setSelection(0);
				
			}
		});
		
		if(mCurrentType == type)
			return;
		
		mLayoutSortMain.setVisibility(View.GONE);
		mLayoutSortMinor.setVisibility(View.VISIBLE);
		
		mData.clear();
		
		switch(type)
		{
		case MINOR_TYPE_CAIPIN:
			for (Sort sort : mCaipins) {
				HashMap<String, Object> hm = new HashMap<String, Object>();
				hm.put("id", sort.id);
				hm.put("name", sort.name);
				mData.add(hm);
			}
			break;
		case MINOR_TYPE_TESE:
			for (Sort sort : mTeses) {
				HashMap<String, Object> hm = new HashMap<String, Object>();
				hm.put("id", sort.id);
				hm.put("name", sort.name);
				mData.add(hm);
			}
			break;
			
		case MINOR_TYPE_KOUWEI:
			for (Sort sort : mKouweis) {
				HashMap<String, Object> hm = new HashMap<String, Object>();
				hm.put("id", sort.id);
				hm.put("name", sort.name);
				mData.add(hm);
			}
			break;
		case MINOR_TYPE_CAIXI:
			for (Sort caixi : mCaixis) {
				HashMap<String, Object> hm = new HashMap<String, Object>();
				hm.put("id", caixi.id);
				hm.put("name", caixi.name);
				mData.add(hm);
			}
			break;
		}
		mAdapter.notifyDataSetChanged();
		mCurrentType = type;
	}
	
	class SortAdapter extends SimpleAdapter {

		public SortAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public long getItemId(int position) {
			Map hm = (Map) getItem(position);
			return Long.valueOf((String) hm.get("id"));

		}
		
		
		
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
           back();
           return true;
        }
        return false;
    }
	
	private void back() {
		 if(mCurrentType == MINOR_TYPE_NONE) {
         	finish();
         }
         else {
         	showSortMain();
         }
	}

	
}
